﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BloxDTO;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace BloxServer2012
{
    public class ServerPeer : PeerBase
    {
        private static readonly object syncRoot = new object();
        public static List<ServerPeer> Players = new List<ServerPeer>();
        public string Username;

        public ServerPeer(IRpcProtocol protocol, IPhotonPeer unmanagedPeer)
            : base(protocol, unmanagedPeer)
        {
            lock (syncRoot)
            {
                BroadcastMessage += this.OnBroadcastMessage;
            }
            Players.Add(this);
        }

        private static event Action<ServerPeer, EventData, SendParameters> BroadcastMessage;

        protected override void OnDisconnect(DisconnectReason disconnectCode, string reasonDetail)
        {
            lock (syncRoot)
            {
                BroadcastMessage -= this.OnBroadcastMessage;
            }
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var response = new OperationResponse(operationRequest.OperationCode);
            switch (operationRequest.OperationCode)
            {
                case OpCode.Login:
                    response = Commands.Login(this, operationRequest[0], operationRequest[1], response);
                    var eventd = new EventData(EventCode.PlayerLogin);
                    eventd.Parameters = new Dictionary<byte, object>();
                    eventd.Parameters.Add(0, operationRequest[0]);
                    BroadcastMessage(this, eventd, sendParameters);
                    break;
                case OpCode.Logout:
                    response = Commands.Logout(this, operationRequest[0], response);
                    eventd = new EventData(EventCode.PlayerLogout);
                    eventd.Parameters.Add(0, operationRequest[0]);
                    BroadcastMessage(this, eventd, sendParameters);
                    break;
                case OpCode.GetWorld:
                    response = Commands.GetWorld(operationRequest[0], response);
                    break;
                case OpCode.GetRegion: ;
                    break;
                case OpCode.GetChunk: ;
                    break;
                case OpCode.GetBlock: ;
                    break;
                case OpCode.PlaceBlock: ;
                    break;
                case OpCode.Auth:
                    response.ReturnCode = 0; // Unknown Op Code
                    break;
                case 229:
                    response.ReturnCode = 0; // Unknown Op Code
                    break;
                default: ;
                    response.ReturnCode = 1; // Unknown Op Code
                    break;
            }

            this.SendOperationResponse(response, sendParameters);   //To sender
        }

        private void OnBroadcastMessage(ServerPeer peer, EventData @event, SendParameters sendParameters)
        {
            if (peer != this)
            {
                this.SendEvent(@event, sendParameters);
            }
        }
    }
}
